package hot100;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/9/13 15:26
 */
public class lc160相交链表 {


    /*
     最正确的思路
        弄这么多字也没讲明白。若相交，链表A： a+c, 链表B : b+c. a+c+b+c = b+c+a+c 。则会在公共处c起点相遇。若不相交，a +b = b+a 。因此相遇处是NULL
     */

    public ListNode solve(ListNode headA,ListNode headB){
        if(headA == null || headB == null){
            return null;
        }
        ListNode pa = headA, pb = headB;
        while(pa != pb){
            pa = pa == null ? headB : pa.next;
            pb = pb == null ? headA : pb.next;
        }
        return pa;
    }




    class ListNode {

        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
